package com.xea.service;

import java.util.List;
import com.uf.util.PageData;
import com.uf.util.SqlHelper;
import com.xea.model.UserInfo;

public class UserInfoService {
	/**
	 * 登录查询用户
	 * @param username  用户名
	 * @param password	用户密码
	 * @return
	 */
	public UserInfo find(String username,String password){
		
		String sql = "select * from UserInfo where username = ? and password = ?";
		Object[] params = new Object[]{username,password};
		List<UserInfo> ls =SqlHelper.query(sql, params, UserInfo.class);
		if(ls == null || ls.isEmpty()){
			return null;
		}
		return (UserInfo)ls.get(0);		

	}
	
	/**
	 * 查询用户是否存在
	 * @param username  用户名
	 * @return
	 */
	public UserInfo find(String username){
		
		String sql = "select * from UserInfo where username = ?";
		Object[] params = new Object[]{username};
		List<UserInfo> ls =SqlHelper.query(sql, params, UserInfo.class);
		if(ls == null || ls.isEmpty()){
			return null;
		}
		return (UserInfo)ls.get(0);		

	}
	
	/**
	 * 查询用户是否存在（排除当前传入id)
	 * @param username  用户名
	 * @return
	 */
	public UserInfo find(String username,int id){
		
		String sql = "select * from UserInfo where username = ? and id!=?";
		Object[] params = new Object[]{username,id};
		List<UserInfo> ls =SqlHelper.query(sql, params, UserInfo.class);
		if(ls == null || ls.isEmpty()){
			return null;
		}
		return (UserInfo)ls.get(0);		

	}
	
	/**
	 * 查询所有用户
	 * @param username   用户名
	 * @param topage     第几页
	 * @return
	 */
	public PageData search(String username,String topage){
		
		String sql = "select * from UserInfo where username like '%"+username+"%' order by id";
		return SqlHelper.queryPageForMysql(sql, null, topage, null, UserInfo.class);	
		
	}
	
	/**
	 * 新增用户
	 * @param UserInfo   用户实体
	 * @return
	 */
	public void add(UserInfo userinfo){
		
		String sql = "insert into UserInfo(name,username,password,type,tel,remark) values(?,?,?,?,?,?)";
		Object[] params=new Object[]{userinfo.getName(),userinfo.getUsername(),userinfo.getPassword(),
									userinfo.getType(),userinfo.getTel(),userinfo.getRemark()};
		SqlHelper.excute(sql, params);
		
	}
	/**
	 * 删除用户
	 * @param id   用户id
	 * @return
	 */
	public void del(String id){
		
		String sql = "delete from UserInfo where id in("+id+")";
		SqlHelper.excute(sql, null);
		
	}
	
	/**
	 * 用户详情
	 * @param id   用户id
	 * @return
	 */
	public UserInfo findById(int id){
		
		String sql = "select * from UserInfo where id = ?";		
		List<UserInfo> list = SqlHelper.query(sql, new Object[]{id}, UserInfo.class);
		if(list == null || list.isEmpty()){
			return null;
		}
		return list.get(0);
		
	}
	
	/**
	 * 修改用户
	 * @param UserInfo   用户实体
	 * @return
	 */
	public void update(UserInfo userinfo){
		
		String sql = "update UserInfo set name=?,username=?,type=?,tel=?,remark=? where id=?";
		Object[] params=new Object[]{userinfo.getName(),userinfo.getUsername(),userinfo.getType(),
									userinfo.getTel(),userinfo.getRemark(),userinfo.getId()};
		SqlHelper.excute(sql, params);
	}
	
	/**
	 * 修改密码
	 * @param UserInfo   用户实体
	 * @return
	 */
	public void updatepassword(UserInfo userinfo){
		
		String sql = "update UserInfo set password=? where id=?";
		Object[] params=new Object[]{userinfo.getPassword(),userinfo.getId()};
		SqlHelper.excute(sql, params);
	}
}
